#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "qi_interpreter.h"
#include "../q_common.h"



struct qi_scriptholder qi_script;





int qi_openscriptfile(const char* const fname, int chunksize)
{
	if(!fname || chunksize <= 0 || !(qi_script.handle = fopen(fname, "rb")))
		return -1;
	qi_script.chunksize = chunksize;
	qi_script.maxpos = qi_script.pos = 0;
	if(!(qi_script.data = (char*)malloc(chunksize * sizeof(char))))
	{
		fclose(qi_script.handle);
		return -1;
	}
	return 0;
}

int qi_getscriptchar()
{
	if(qi_script.pos == qi_script.maxpos)
	{
		qi_script.pos = 0;
		if(!(qi_script.maxpos = fread(qi_script.data, qi_script.chunksize, 1, qi_script.handle)))
		{
			free(qi_script.data);
			return -1;
		}
	}

	return (int)qi_script.data[qi_script.pos++];
}



int initinterpreter()
{
	//if(!(qi_root = (struct qi_token*)malloc(sizeof(struct qi_token))))
	//	return Q_STATUS_FATAL;


	return 0;	
}





